/**
 * 公司单位人员管理
 * @class DepUserView
 * @extends Ext.Panel
 */
DepUserView=Ext.extend(Ext.Panel,{
	constructor:function(conf){
		Ext.apply(this,conf);
		this.initUI();
		DepUserView.superclass.constructor.call(this,{
			id:'DepUserView',
			title:'工作人员管理',
			layout:'border',
			iconCls:'menu-department',
			items:[
				this.leftPanel,
				this.centerPanel
			]
		});
	},
	initUI:function(){
		this.depTreePanel=new Ext.ux.TreePanelEditor({
			treeType:'org',
			url : __ctxPath+'/system/treeDepartment.do',
			scope : this,
			autoScroll:true,
			onclick:this.depTreeClick
		});
		
		this.leftPanel = new Ext.Panel({
		 	region:'west',
		 	title:'组织架构',
		 	layout:'fit',
		 	collapsible : true,
			split : true,
			border:false,
			width : 240,
			items:[this.depTreePanel]
		});
		//搜索用户列
	 	this.searchPanel=new KE.SearchPanel({
		 		region:'north',
				items : [{
					text:'账号'
				},{
					xtype:'textfield',
					width:150,
					name:'Q_username_S_LK'
				},{
					text:'姓名'
				},{
					xtype:'textfield',
					width:150,
					name:'Q_fullname_S_LK'
				},{
					xtype:'button',
					iconCls:'btn-search',
					text:'查询',
					scope:this,
					handler:function(){
						$search({
							searchPanel : this.searchPanel,
							gridPanel : this.userGridPanel
						});
					}
				}, {
					xtype : 'button',
					text : '清空',
					scope : this,
					iconCls : 'reset',
					handler : function() {
						this.searchPanel.getForm().reset();
					}
				}]
	 	});
		this.userGridPanel=new KE.GridPanel({
			region:'center',
			tbar:[{
					iconCls:'btn-add',
					text:'添加人员',
					scope:this,
					handler:this.addUser
				},'-',{
					iconCls:'btn-del',
					text:'删除人员',
					scope:this,
					handler:this.removeUserOrg
				}
			],
			title:'工作人员列表',
			url:__ctxPath+'/system/depUsersAppUser.do',
			fields:[
				'userId','fullname','username','roleNames', 'status', 'depNames'
			],
			columns:[{
						header : 'userId',
						dataIndex : 'userId',
						hidden : true
					}, {
						header : '状态',
						dataIndex : 'status',
						width : 30,
						renderer : function(value) {
							var str = '';
							if (value == 1) {
								str += '<img title="激活" src="' + __ctxPath
										+ '/images/flag/customer/effective.png"/>'
							} else {
								str += '<img title="禁用" src="' + __ctxPath
										+ '/images/flag/customer/invalid.png"/>'
							}
							return str;
						}
					}, {
						header:'账号',
						dataIndex:'username'
					},{
						header:'姓名',
						dataIndex:'fullname'
					}, {
						header : '拥有角色',
						dataIndex : 'roleNames',
						sortable : false
					}, {
						header : '所属组织',
						dataIndex : 'depNames',
						sortable : false,
						width : 120
					},{
						header : '管理',
						width : 60,
						xtype: 'actioncolumn',
						items: [{
							iconCls : 'btn-edit',
							tooltip : '编辑',
							scope : this,
							handler : this.editRs
						}]
					}]
		});
		this.centerPanel = Ext.Panel({
			region: 'center',
			layout: 'border',
			items: [this.searchPanel, this.userGridPanel]
		});
	},
	//重新加载树
	reloadDep:function(){
		this.depTreePanel.store.reload();
	},
	//单位树点击
	depTreeClick:function(){
		var depId=this.depTreePanel.selectedNode.id;
		var store= this.userGridPanel.getStore();
		store.proxy.extraParams={depId:depId};
		store.reload(); 
	},
	//行编辑
	onRowAction:function(grid, record, action, row, col){
		switch (action) {
			case 'btn-del' :
				this.removeRs.call(this, record.data.userId);
				break;
			case 'btn-edit' :
				this.editRs.call(this, record);
				break;
			case 'btn-relativeJob': 
				this.addRelativeUser.call(this, record);
				break;
			default :
				break;
		}
	},
	//编辑用户信息
	editRs:function(grid, row, col){
		var record = grid.getStore().getAt(row);
		App.clickTopTab('UserFormPanel_'+record.data.userId,{userId:record.data.userId,username:record.data.username});
	},
	addUser:function(){
		App.clickTopTab('UserFormPanel');
	},
	// 删除单位人员
	removeUserOrg : function() {
		var grid = this.userGridPanel;
		var selectRecords = grid.getSelectionModel().getSelection();
		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
			return;
		}
		if(this.depTreePanel.selectedNode==null){
			Ext.ux.Toast.msg("信息", "请选择组织！");
			return;
		}
		var ids = Array();
		for (var i = 0; i < selectRecords.length; i++) {
			ids.push(selectRecords[i].data.userId);
		}
		Ext.Msg.confirm('删除操作', '注意：要删除该人员与单位的关系吗？', function(btn) {
			if (btn == 'yes') {
				Ext.Ajax.request({
					url : __ctxPath + '/system/multiDelUserOrg.do',
					method : 'post',
					params : {
						ids : ids.toString(),
						orgId:this.depTreePanel.selectedNode.id
					},
					success : function(response) {
						var result = Ext.util.JSON.decode(response.responseText);
						if(result.msg == null){
							Ext.ux.Toast.msg("操作信息", "人员删除成功");
						}
						grid.getStore().reload();
					},
					failure : function() {
						Ext.ux.Toast.msg("操作信息", "人员删除失败");
					}
				});
			}
		},this);
	},
	//添加组织的人员
	addDepUser:function(){
		var orgId=0;
		var selNode=this.depTreePanel.selectedNode;
		if(selNode){
			orgId=selNode.id;
		}
		if(orgId==0){
			Ext.ux.Toast.msg('操作信息','请选择对应的单位!');
			return;
		}
		//弹出用户选择器，根据当前选择的节点，把用户加入该组织或单位
		new UserDialog({
			title:'加入新用户',
			scope:this,
			single:false,
			callback:function(ids,names){
				Ext.Ajax.request({
					method:'POST',
					scope:this,
					url:__ctxPath+'/system/addOrgsUserOrg.do',
					params:{
						userIds:ids,
						orgId:orgId
					},
					success:function(resp,options){
						var store= this.userGridPanel.getStore();
						store.proxy.extraParams={depId:orgId};
						this.userGridPanel.getBottomToolbar().moveFirst();
					}
				});
			}
		}).show();
	}
});